package com.xinsoft.controller.basic;

import com.xinsoft.common.Response;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.SysProjectPageParam;
import com.xinsoft.entity.po.SysProject;
import com.xinsoft.service.SysProjectService;
import com.xinsoft.service.SysDocumentNumService;
import com.xinsoft.validator.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/system/project")
public class SysProjectController {

    @Autowired
    private SysProjectService sysProjectService;

    @Autowired
    private SysDocumentNumService sysDocumentNumService;

    /**
     * 新增
     */
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> add(@RequestBody SysProject sysProject) {
        // 参数校验
        ValidatorUtils.validateEntity(sysProject);

        sysDocumentNumService.incrementLastSerialByDocumentId(Const.DOCUMENT_ID.SYS_PJ_DOCUMENT);

        return Response.succeed(sysProjectService.addSysProject(sysProject));
    }

    /**
     * 更新
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/edit")
    public Response<?> edit(@RequestBody  SysProject sysProject) {
        // 参数校验
        ValidatorUtils.validateEntity(sysProject);
        return Response.succeed(sysProjectService.editSysProject(sysProject));
    }

    /**
     * 获取客户
     */
    @GetMapping("/detail")
    public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        return Response.succeed(sysProjectService.getSysProject(id));
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/del")
    public Response<?> del(@RequestBody List<Integer> ids) {
        sysProjectService.del(ids);
        return Response.succeed();
    }

    /**
     * 审核
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/audit")
    public Response<?> audit(@RequestBody List<Integer> ids) {
        sysProjectService.audit(ids);
        return Response.succeed();
    }

    /**
     * 反审核
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/deAudit")
    public Response<?> deAudit(@RequestBody List<Integer> ids) {
        sysProjectService.deAudit(ids);
        return Response.succeed();
    }

    /**
     * 分页
     */
    @PostMapping("/pageList")
    public Response<?> pageList(@RequestBody SysProjectPageParam sysProjectPageParam) {
        return Response.succeed(sysProjectService.findPageListByParam(sysProjectPageParam));
    }
}
